package cc.vv.baselibrary.util.download;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import org.jetbrains.annotations.NotNull;

import cc.vv.baselibrary.view.JniCancelAndConfirmDialog;
import cc.vv.baselibrary.view.JniDownloadProgressBarDialog;
import zlc.season.rxdownload3.core.Failed;
import zlc.season.rxdownload3.core.Status;
import zlc.season.rxdownload3.core.Succeed;


/**
 * 下载应用apk包的Dialog
 * Created by sun.li on 2017/10/18.
 *
 * @author sun.li
 */

public class DownloadApkUtil {

    private Context mContext;
    /**
     * 版本对比地址
     */
    private ApkDownloadInfoObj mAppVersion;

    /**
     * 下载apk的对话框
     */
    private JniDownloadProgressBarDialog jniDownloadProgressBarDialog;

    /**
     * 下载提示框Dialog对象
     */
    private JniCancelAndConfirmDialog jniCancelAndConfirmDialog;

    /**
     * 版本更新标志，有更新"Yes"
     */
    public static final String UpdateMarkYes = "Yes";

    private OnButtonClick onButtonClick;

    public DownloadApkUtil(Context context, ApkDownloadInfoObj mAppVersion) {
        this.mAppVersion = mAppVersion;
        this.mContext = context;
        //初始化下载进度弹窗
        initDownloagDialog();
    }

    public void initDownloagDialog() {
        if (jniDownloadProgressBarDialog == null) {
            jniDownloadProgressBarDialog = new JniDownloadProgressBarDialog(mContext);
        }
        jniDownloadProgressBarDialog.setTitle("正在下载");
        jniDownloadProgressBarDialog.setCancelable(false);
    }
    
    public void downLoad() {
        if (mAppVersion != null && !TextUtils.isEmpty(mAppVersion.getUrl())) {
            //开始执行下载
            jniDownloadProgressBarDialog.show();
            DownloadManager.INSTANCE.downloadApk(mAppVersion.getUrl(), mContext, new DownloadManager.DownLoadInterface() {
                @Override
                public void downLoadError(@NotNull Object error) {
                    if (onButtonClick != null) {
                        onButtonClick.negativeClick();
                    }
                }

                @Override
                public void downLoadStatus(@NotNull Status status) {
                    if (status instanceof Succeed) {
                        jniDownloadProgressBarDialog.setProgressBar(100);
                        jniDownloadProgressBarDialog.dismiss();
                    } else if (status instanceof Failed) {
                        //下载失败
                        if (onButtonClick != null) {
                            onButtonClick.negativeClick();
                        }
                    } else {
                        double pro = Double.parseDouble(status.percent().replace("%", ""));
                        jniDownloadProgressBarDialog.setProgressBar((int) pro);
                    }
                }
            });
        }
    }

    /**
     * 显示更新提示的Dialog
     */
    public void showUpdateDialog() {
        if (jniCancelAndConfirmDialog == null) {
            jniCancelAndConfirmDialog = new JniCancelAndConfirmDialog(mContext);
        }
        jniCancelAndConfirmDialog.setTitle("有新版本");
        jniCancelAndConfirmDialog.setCancelable(false);
        jniCancelAndConfirmDialog.setContent(mAppVersion.getContent());
        jniCancelAndConfirmDialog.setConfirmBtnText("下载");
//        if ("1".equals(mAppVersion.getForcedUpdatingState())) {
//            jniCancelAndConfirmDialog.setCancelBtnText("");
//        } else {
//            jniCancelAndConfirmDialog.setCancelBtnText("忽略");
//        }
        jniCancelAndConfirmDialog.setEventInterface(new JniCancelAndConfirmDialog.EventInterface() {
            @Override
            public void cancelOnClick() {
                if (onButtonClick != null) {
                    onButtonClick.negativeClick();
                }
                if (jniCancelAndConfirmDialog != null) {
                    jniCancelAndConfirmDialog.dismiss();
                }
            }

            @Override
            public void confirmOnClick() {
                if (onButtonClick != null) {
                    onButtonClick.positiveClick();
                }
                if (jniCancelAndConfirmDialog != null) {
                    jniCancelAndConfirmDialog.dismiss();
                }
            }
        });
        jniCancelAndConfirmDialog.show();
    }

    /**
     * 设置按钮监听
     *
     * @param buttonClick 按钮点击事件监听回调
     */
    public void setOnButtonClick(OnButtonClick buttonClick) {
        this.onButtonClick = buttonClick;
    }

    /**
     * Button点击回调
     */
    public interface OnButtonClick {
        /**
         * 确认按钮点击
         */
        void positiveClick();

        /**
         * 取消按钮点击
         */
        void negativeClick();
    }

}
